package com.cljh.service.impl;

import com.cljh.bean.Admin;
import com.cljh.bean.Files;
import com.cljh.bean.Role;
import com.cljh.bean.Saler;
import com.cljh.controller.page.PageBean;
import com.cljh.controller.page.ProductPageBean;
import com.cljh.mapper.AdminMapper;
import com.cljh.mapper.FileMapper;
import com.cljh.mapper.SalerMapper;
import com.cljh.service.AdminService;
import com.cljh.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    FileMapper fileMapper;
    @Autowired
    SalerMapper salerMapper;
    @Override
    public boolean save(Admin admin) {
        int save = adminMapper.save(admin);
        Integer id = admin.getId();
        Saler saler = new Saler();
        saler.setAdminId(id);
        salerMapper.save(saler);
        return save>0;
    }

    @Override
    public Admin selectByUsername(Admin admin) {
        return adminMapper.selectByUsername(admin);
    }

    @Override
    public Admin select(Admin admin) {

        return adminMapper.select(admin);
    }

    @Override
    public boolean upd(Admin admin) {

        return adminMapper.upd(admin)>0;
    }

    @Override
    public Admin getById(Integer id) {
        return adminMapper.getById(id);
    }

    @Override
    public PageBean<Admin> selectAllByCondition(Integer currentPage, Integer pageSize, Admin admin) {

        //计算开始索引
        int begin = (currentPage - 1) * pageSize;

        String username = admin.getUsername();
        if (username != null && username.length() > 0) {
            admin.setUsername("%" + username + "%");
        }

        List<Admin> admins = adminMapper.selectAllByCondition(begin, pageSize, admin);
        int i = adminMapper.selectCount(admin);
        PageBean<Admin> adminPageBean = new PageBean<>();
        adminPageBean.setRows(admins);
        adminPageBean.setTotalCount(i);


        return adminPageBean;
    }

    @Override
    public boolean deleteId(Integer id) {
        return adminMapper.deleteId(id)>0;
    }

    @Override
    public boolean deleteIds(List<Integer> ids) {
        return adminMapper.deleteIds(ids)>0;
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public List<Admin> selectAll() {
        return adminMapper.selectAll();
    }

    @Override
    public boolean saveAll(List<Admin> adminList) {

        return adminMapper.saveAll(adminList)>0;
    }

    @Override
    public Admin selectByEmail(String username, String emailCode) {
        return adminMapper.selectByEmail(username,emailCode);
    }

    @Override
    public Admin selectByEmailCode(String email) {

        return adminMapper.selectByEmailCode(email);
    }
}
